package cn.gbase.jiangsu.data.transfer.service;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.GoodesOrderDetailView;
import cn.gbase.jiangsu.data.transfer.bean.OrderSupplierView;
import cn.gbase.jiangsu.data.transfer.bean.Product;
import cn.gbase.jiangsu.data.transfer.bean.ShopUser;
import cn.gbase.jiangsu.data.transfer.bean.SupplierToCommunity;

@IocBean(args = { "refer:dao" })
public class PrintInfoService extends IdEntityService<SupplierToCommunity> {
	@Inject
	private ProductService productService;

	public PrintInfoService() {
		super();
	}

	public PrintInfoService(Dao dao) {
		super(dao);
	}

	//打印小票的LIST页面
	public QueryResult printSmallBillListService(int communityId, final String communityName, int pageNum,
			int pageSize, String dingdanshijian) {
		String sqlStr = "";
		if (dingdanshijian == null || "".equals(dingdanshijian)) {
			sqlStr = "select product_count,sell_price,product_name,create_time,code from order_supplier_view "
					+ "where community_id = @communityId and order_type = @orderType" + " group by create_time,code";
		} else {
			sqlStr = "select product_count,sell_price,product_name,create_time,code from order_supplier_view "
					+ "where create_time like '%" + dingdanshijian
					+ "%' and community_id = @communityId and order_type = @orderType" + " group by create_time,code";
		}

		Sql sql = Sqls.create(sqlStr);
		sql.params().set("communityId", communityId).set("orderType", 1);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(java.sql.Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<OrderSupplierView> supplierToCommunityList = new ArrayList<OrderSupplierView>();
				while (rs.next()) {
					OrderSupplierView orderSupplierView = new OrderSupplierView();
					orderSupplierView.setCreateTime(rs.getDate("create_time"));
					orderSupplierView.setCode(rs.getString("code"));
					orderSupplierView.setCommunityName(communityName);
					supplierToCommunityList.add(orderSupplierView);
				}
				return supplierToCommunityList;
			}
		});
		this.dao().execute(sql);

		String sqlStr1 = "select count(*) from (select * from order_supplier_view "
				+ "where community_id = @communityId and order_type = @orderType" + " group by create_time,code) t1";
		Sql sql1 = Sqls.create(sqlStr1);
		sql1.params().set("communityId", communityId).set("orderType", 1);
		sql1.setCallback(Sqls.callback.integer());
		this.dao().execute(sql1);
		Pager pager = this.dao().createPager((pageNum < 1) ? 1 : pageNum, (pageSize < 1) ? 10 : pageSize);
		pager.setRecordCount(sql1.getObject(Integer.class));
		QueryResult result = new QueryResult();
		result.setList(sql.getList(OrderSupplierView.class));
		result.setPager(pager);
		return result;
	}

	//根据订单ID和订单时间获得当前订单当前时间的该服务站的所有订单
	public Map printSmallBillService(String code) {
		List<GoodesOrderDetailView> goodesOrderDetailViewList = this.dao().query(GoodesOrderDetailView.class,
				Cnd.where("code", "=", code));
		Map resultMap = new HashMap();
		String orderAddress = "";
		Date actualArriveDate = null;
		Integer orderUserId = null;
		Date createDate = null;
		//运费
		BigDecimal yunfei = new BigDecimal("0.00");
		//优惠
		BigDecimal youhui = new BigDecimal("0.00");
		//商品合计
		BigDecimal shangpinheji = new BigDecimal("0.00");
		//当前这笔订单的合计
		BigDecimal orderCountNum = null;
		for (GoodesOrderDetailView goodesOrderDetailView : goodesOrderDetailViewList) {
			//每个商品的总价
			goodesOrderDetailView.setZongjiEvery(goodesOrderDetailView.getUnitPrice().multiply(
					new BigDecimal(goodesOrderDetailView.getProductCount())));
			//商品名称
			goodesOrderDetailView.setShowProductName(getProductNameById(goodesOrderDetailView.getProductId()));
			//配送地址
			orderAddress = goodesOrderDetailView.getAddress();
			//实际送达时间
			actualArriveDate = goodesOrderDetailView.getActualArriveDate();
			orderUserId = goodesOrderDetailView.getUserId();
			createDate = goodesOrderDetailView.getCreateTime();
			if (yunfei.compareTo(new BigDecimal("0.00")) == 0) {
				//运费
				if (goodesOrderDetailView.getDeliveryPrice() != null) {
					yunfei = goodesOrderDetailView.getDeliveryPrice();
				}
			}
			if (youhui.compareTo(new BigDecimal("0.00")) == 0) {
				if (goodesOrderDetailView.getDiscountPrice() != null) {
					//优惠
					youhui = goodesOrderDetailView.getDiscountPrice();
				}
			}
			shangpinheji = goodesOrderDetailView.getSellPrice();
		}
		//根据USERID获得业主姓名
		if (orderUserId != null) {
			ShopUser shopUser = this.dao().fetch(ShopUser.class, Cnd.where("id", "=", orderUserId));
			resultMap.put("realName", shopUser.getRealName());
		} else {
			resultMap.put("realName", "");
		}
		resultMap.put("goodesOrderDetailViewList", goodesOrderDetailViewList);
		resultMap.put("orderAddress", orderAddress);
		resultMap.put("orderCountNum", shangpinheji);
		resultMap.put("actualArriveDate", actualArriveDate);
		resultMap.put("createDate", createDate);
		resultMap.put("yunfei", yunfei);
		resultMap.put("youhui", youhui);
		resultMap.put("zuihouheji", shangpinheji.add(yunfei).subtract(youhui));
		return resultMap;
	}

	public String getProductNameById(Integer productId) {
		Product product = productService.getProductById(productId);
		if (product != null) {
			if (product.getProductName().length() > 6) {
				return product.getProductName().substring(0, 6);
			} else {
				return product.getProductName();
			}
		} else {
			return "";
		}

	}
}
